package com.jeestudio.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {

	/**
	 * @param args
	 * @throws ParseException
	 */
	public static void main(String[] args) throws ParseException {
		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(getYesterDateEnd()));

		System.out.println(getFrontDay(new Date(),2,"end"));
	}
	//返回某个日期下几天的日期
    public static Date getNextDay(Date date, int i) {
		 Calendar cal = new GregorianCalendar();
		 cal.setTime(date);
		 cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i);
		 return cal.getTime();
	}
  	//返回某个日期前几天的日期
    public static Date getFrontDay(Date date, int i, String flag)  {
		Calendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			SimpleDateFormat sdfBegin = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if("begin".equals(flag)){
				return sdfBegin.parse(sdf.format(cal.getTime())+" 00:00:00");
			}else {
				return sdfBegin.parse(sdf.format(cal.getTime())+" 23:59:59");
			}
		} catch (Exception e) {
		    e.printStackTrace();
			return null;
		}
	 }
	public static String getBeforeDate(int months) {
		Date dNow = new Date(); // 当前时间
		Date dBefore = new Date();
		Calendar calendar = Calendar.getInstance(); // 得到日历
		calendar.setTime(dNow);// 把当前时间赋给日历
		calendar.add(calendar.MONTH, -months); // 设置为前3月
		dBefore = calendar.getTime(); // 得到前3月的时间

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 设置时间格式
		String defaultStartDate = sdf.format(dBefore); // 格式化前3月的时间
		String defaultEndDate = sdf.format(dNow); // 格式化当前时间
		return defaultStartDate;
	}

	/**
	 * 计算两个日期之间相差的天数
	 * 
	 * @param smdate
	 *            较小的时间
	 * @param bdate
	 *            较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static int daysBetween(Date smdate, Date bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		smdate = sdf.parse(sdf.format(smdate));
		bdate = sdf.parse(sdf.format(bdate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 字符串的日期格式的计算
	 */
	public static int daysBetween(String smdate, String bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(smdate));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(bdate));
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 字符串的日期格式的计算
	 */
	public static int yearsBetween(String smdate, String bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(smdate));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(bdate));
		long time2 = cal.getTimeInMillis();
		long between_years = (time2 - time1) / (1000 * 3600 * 24 * 365);

		return Integer.parseInt(String.valueOf(between_years));
	}

	public static String getNowDateString(Date now) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(now);
	}

	public static String getNowDateStringForxxx(Date now) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(now);
	}

	// 计算两个日期相差年数
	public static int yearDateDiff(String startDate, String endDate) {
		Calendar calBegin = Calendar.getInstance(); // 获取日历实例
		Calendar calEnd = Calendar.getInstance();
		calBegin.setTime(stringTodate(startDate, "yyyy-MM-dd")); // 字符串按照指定格式转化为日期
		calEnd.setTime(stringTodate(endDate, "yyyy-MM-dd"));
		return calEnd.get(Calendar.YEAR) - calBegin.get(Calendar.YEAR);
	}

	// 字符串按照指定格式转化为日期
	public static Date stringTodate(String dateStr, String formatStr) {
		// 如果时间为空则默认当前时间
		Date date = null;
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		if (dateStr != null && !dateStr.equals("")) {
			String time = "";
			try {
				Date dateTwo = format.parse(dateStr);
				time = format.format(dateTwo);
				date = format.parse(time);
			} catch (ParseException e) {
				e.printStackTrace();
			}

		} else {
			String timeTwo = format.format(new Date());
			try {
				date = format.parse(timeTwo);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return date;
	}

	/**
	 * 两个字符串日期相隔月份数
	 * 
	 * @param date1
	 *            <String>
	 * @param date2
	 *            <String>
	 * @return int
	 * @throws ParseException
	 */
	/**
	 * 计算两个日期之间相差的月数
	 * 
	 * @return
	 * @throws ParseException
	 */
	public static int getMonthSpace(String start, String end) throws ParseException {
		int iMonth = 0;
		int flag = 0;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date1 = format.parse(start);
		Date date2 = format.parse(end);
		try {
			Calendar objCalendarDate1 = Calendar.getInstance();
			objCalendarDate1.setTime(date1);

			Calendar objCalendarDate2 = Calendar.getInstance();
			objCalendarDate2.setTime(date2);

			if (objCalendarDate2.equals(objCalendarDate1))
				return 0;
			if (objCalendarDate1.after(objCalendarDate2)) {
				Calendar temp = objCalendarDate1;
				objCalendarDate1 = objCalendarDate2;
				objCalendarDate2 = temp;
			}
			if (objCalendarDate2.get(Calendar.DAY_OF_MONTH) < objCalendarDate1.get(Calendar.DAY_OF_MONTH))
				flag = 1;

			if (objCalendarDate2.get(Calendar.YEAR) > objCalendarDate1.get(Calendar.YEAR))
				iMonth = ((objCalendarDate2.get(Calendar.YEAR) - objCalendarDate1.get(Calendar.YEAR)) * 12
						+ objCalendarDate2.get(Calendar.MONTH) - flag) - objCalendarDate1.get(Calendar.MONTH);
			else
				iMonth = objCalendarDate2.get(Calendar.MONTH) - objCalendarDate1.get(Calendar.MONTH) - flag;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return iMonth;
	}
	
	 //获取明天的开始时间
    public static Date getBeginDayOfTomorrow() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, 1);

        return cal.getTime();
    }

	//获取当天的开始时间
	public static Date getDayBegin() {
		Calendar cal = new GregorianCalendar();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	//获取当天的结束时间
	public static Date getDayEnd() {
		Calendar cal = new GregorianCalendar();
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MILLISECOND, 99);
		return cal.getTime();
	}
	//获取昨天开始时间
	public static Date getYesterDateBegin(){
		Calendar cal = new GregorianCalendar();
		cal.setTime(getDayBegin());
		cal.add(Calendar.DAY_OF_MONTH, -1);
		return cal.getTime();
	}

	//获取十分钟内的数据开始时间
	public static Date getOneHourDateBegin(int minute){
		Calendar cal = new GregorianCalendar();
		cal.setTime(new Date());
		cal.add(Calendar.MINUTE, -minute);
		return cal.getTime();
	}


	//获取昨天结束时间
	public static Date getYesterDateEnd(){
		Calendar cal = new GregorianCalendar();
		cal.setTime(getDayEnd());
		cal.add(Calendar.DAY_OF_MONTH, -1);
		return cal.getTime();
	}
	//获取字符串类型的昨天结束时间
	public static String getYesterDateEndStr(){
		Date  yesterDate = getYesterDateEnd();
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(yesterDate);
	}
	//获取字符串类型的昨天开始时间
	public static String getYesterDateBeginStr(){
		Date yesterDate = getYesterDateBegin();
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(yesterDate);
	}
	//获取字符串类型的当天结束时间
	public static String getDayEndStr(){
		Date  yesterDate = getDayEnd();
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(yesterDate);
	}
	//获取字符串类型的当天开始时间
	public static String getDayBeginStr(){
		Date yesterDate =  getDayBegin();
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(yesterDate);
	}

}